//package sc.test.spring.quartz;
//
//import java.util.Date;
//import java.util.Set;
//
//import org.quartz.CronScheduleBuilder;
//import org.quartz.JobBuilder;
//import org.quartz.JobDetail;
//import org.quartz.JobKey;
//import org.quartz.Scheduler;
//import org.quartz.Trigger;
//import org.quartz.TriggerBuilder;
//import org.quartz.TriggerKey;
//import org.quartz.impl.matchers.GroupMatcher;
//
//import sc.common.test.DateUtil;
//import sc.common.test.TestUtil;
//import sc.common.test.cmd.CommandContext;
//import sc.common.test.cmd.Commander;
//
//public class CoolCommander implements Commander {
//
//    Scheduler sched;
//    int count = 0;
//
//    @SuppressWarnings("unchecked")
//    @Override
//    public void commandReceived(CommandContext ctxt) {
//        String command = ctxt.getCommand();
//        String[] args = ctxt.getArgs();
//        if (args == null) {
//            args = new String[0];
//        }
//        if (command == null) {
//            return;
//        }
//        try {
//            if ("help".equals(command)) {
//                TestUtil.sp("");
//            } else if ("p".equals(command)) {
//                TestUtil.sp("[op]pause");
//                sched.pauseAll();
//            } else if ("r".equals(command)) {
//                TestUtil.sp("[op]resume");
//                sched.resumeAll();
//            } else if ("d".equals(command)) {
//                TestUtil.sp("[op]delete");
//                Set<JobKey> jks = sched.getJobKeys(GroupMatcher.groupStartsWith(""));
//                for (JobKey jk : jks) {
//                    TestUtil.sp("delete " + jk);
//                    sched.deleteJob(jk);
//                }
//            } else if ("a".equals(command)) {
//                TestUtil.sp("[op]add");
//                count++;
//                JobBuilder jobBuilder = JobBuilder.newJob(CoolJob.class).withIdentity("jobname" + count,
//                        "jobgroup" + count);
//                @SuppressWarnings("rawtypes")
//                TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger().withIdentity("tgname" + count,
//                        "tggroup" + count);
//                boolean schedFlag = true;
//                if (args.length != 0 && args[0].equals("1")) {
//                    JobKey simple = new JobKey("simple", "simple");
//                    if (sched.getJobDetail(simple) != null) {
//                        sched.triggerJob(simple);
//                        TestUtil.sp("Trigger job " + simple);
//                        schedFlag = false;
//                    } else {
//                        jobBuilder = jobBuilder.withIdentity("simple", "simple");
//                        triggerBuilder = triggerBuilder.withIdentity("simple", "simple")
//                                .startAt(DateUtil.parseMutely("2012-07-20 00:00:00"))
//                                .usingJobData(CoolJob.DELAY_SECONDS, 5);
//                    }
//                } else {
//                    triggerBuilder = triggerBuilder
//                            .withSchedule(
//                                    CronScheduleBuilder.cronSchedule("0/2 * * * * ?")
//                                            .withMisfireHandlingInstructionDoNothing()).usingJobData(
//                                    CoolJob.DELAY_SECONDS, 3);
//                }
//                JobDetail job = jobBuilder.build();
//                if (schedFlag) {
//                    Date ft = sched.scheduleJob(job, triggerBuilder.build());
//                    TestUtil.sp(job + " has been scheduled to run at: " + ft);
//                }
//            } else if ("e".equals(command)) {
//                TestUtil.sp("[op]exit");
//                ctxt.setExit(true);
//            } else if ("s".equals(command)) {
//                StringBuilder sb = new StringBuilder("[op]show\r\n");
//                Set<JobKey> jks = sched.getJobKeys(GroupMatcher.groupStartsWith(""));
//                sb.append("Jobs: ").append(jks.size()).append("\r\n");
//                int countjk = 0;
//                for (JobKey jk : jks) {
//                    countjk++;
//                    JobDetail jd = sched.getJobDetail(jk);
//                    sb.append("(").append(countjk).append(") ").append(jk).append(", curExeDisallow = ")
//                            .append(jd.isConcurrentExectionDisallowed()).append("\r\n");
//                }
//
//                Set<TriggerKey> tks = sched.getTriggerKeys(GroupMatcher.groupStartsWith(""));
//                sb.append("Triggers: ").append(tks.size()).append("\r\n");
//                int counttk = 0;
//                for (TriggerKey tk : tks) {
//                    counttk++;
//                    Trigger t = sched.getTrigger(tk);
//                    sb.append("(").append(counttk).append(") ").append(t.getKey()).append(", triggerJob = ")
//                            .append(t.getJobKey()).append("\r\n");
//                }
//                TestUtil.sp(sb.toString());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    public CoolCommander(Scheduler sched) {
//        super();
//        this.sched = sched;
//    }
//
//}
